/**
 * axios基础构建
 * @date 2019-12-24
 */

 import Vue from 'vue';
import { LoginBean } from '../../../Bean/LoginBean';
import storage from '../../storage';
 import getUrl from './AxiosConfig';
 import instance from './AxiosIntercept';
 import { AxiosRequest, CustomResponse, WxResponse } from './AxiosInterface';
 
 class Abstract {
     // 外部传入的baseUrl
     protected baseURL: string = 'http://api.wuxiakj.com';
    //  protected baseURL: string = process.env.VUE_APP_BaseURL;
     // 自定义header头
     protected headers: object = {
        'Content-Type':   'text/plain',
        // 'Content-Length': '268',
        //  ContentType: 'application/json;charset=UTF-8'
     }
 
     private apiAxios({ baseURL = this.baseURL, headers = this.headers, method, url, data, params, responseType, needToken = true }: AxiosRequest): Promise<WxResponse> {
 
         // url解析
        //  const _url = (url as string).split('.');
        //  url = getUrl(_url[0], _url[1]);
        if (needToken) params = { ...params, Token: storage().get('token') || '' } // 插入token
         return new Promise((resolve, reject) => {
             instance({
                 baseURL,
                 headers,
                 method,
                 url,
                 params,
                 data,
                 responseType
             }).then((res) => {
                 // 200:服务端业务处理正常结束
                 if (res.status === 200) {
                     if (res.data.status) {
                        resolve(res.data);
                     } else {
                        //  Vue.prototype.$message({ type: 'error', message: res.data?.errorMessage || (url + '请求失败') });
                         resolve(res.data);
                     }
                 } else {
                     resolve(res.data);
                 }
             }).catch((err) => {
                 const message = err?.data?.errorMessage || err?.message || (url + '请求失败');
                //  Vue.prototype.$message({ type: 'error', message });
                 // eslint-disable-next-line
                 reject({ status: false, message, data: null});
             });
         });
     }
 
     /**
      * GET类型的网络请求
      */
     protected getReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
         return this.apiAxios({ baseURL, headers, method: 'GET', url, data, params, responseType });
     }
 
     /**
      * POST类型的网络请求
      */
     protected postReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest): any {
         return this.apiAxios({ baseURL, headers, method: 'POST', url, data, params, responseType });
     }
 
     /**
      * PUT类型的网络请求
      */
     protected putReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
         return this.apiAxios({ baseURL, headers, method: 'PUT', url, data, params, responseType });
     }
 
     /**
      * DELETE类型的网络请求
      */
     protected deleteReq({ baseURL, headers, url, data, params, responseType }: AxiosRequest) {
         return this.apiAxios({ baseURL, headers, method: 'DELETE', url, data, params, responseType });
     }
 }
 
 export default Abstract;
 